home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9147 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: alterdial.uu.net!not-for-mail
  2. From: rogerst@approach.com (Tom Rogers)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Tradition or what?
  5. Date: Fri, 08 Mar 1996 16:36:56 GMT
  6. Message-ID: <4hpd8a$d70@alterdial.UU.NET>
  7. References: <4g0elg$mdr@redstone.interpath.net> <4h0bbo$c0q@fohnix.metronet.com> <4h7ce0$ojd@news.interpath.net> <4h85sm$lm0@texas.nwlink.com> <danpop.825961872@rscernix> <313C5CD1.1209@oc.com> <4hmos4$ij2@news.interpath.net>
  8. NNTP-Posting-Host: 198.177.211.32
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. softbase@mercury.interpath.net (Scott McMahan - Softbase Systems)
  12. wrote:
  13.  
  14. >Larry Weiss (lfw@oc.com) wrote:
  15. >  
  16. >: > Actually, the second rule is quite good (except that '2' should be replaced
  17. >: > by '1' :-)  Magic constants embedded in the code are a huge nuisance
  18. >: > for maintenability.  [...]
  19.  
  20. >: I agree that there are good uses for giving a literal a name, and using
  21. >: that name instead of repeated usage of the literal, but to make a
  22. >: mandate that even if the literal were referenced only once you must give
  23. >: it a name would seem too extreme.
  24.  
  25. >From personal experience, I think you can't be too extreme. I've been
  26. >burned by literals before. I've come to the conclusion that magic
  27. >numbers in the code are evil and should be done away with. Anything
  28. >that isn't a universal, unchanging constant (like an integer being 32
  29. >bits :)), something like a law of physics, needs to be a constant in
  30. >the program. I always #define it, too, since C has broken const
  31. >constants that can't be used as initializers (which defeats the whole
  32. >point of const).
  33.  
  34. >Scott
  35.  
  36. I agree, Scott.   Magic numbers are evil.  Don't use them not even
  37. once.  Your code will be reused, by you or someone else.  Even if
  38. you just use a magic number in one place it doesn't mean that in
  39. the future your routines won't get included in some other module
  40. with other new routines that need to use the 'same' magic number.
  41.  
  42. Furthermore, what does 5 or 7 or 10 mean?  It is NOT self documenting.
  43. Make it a well named constant and reduce the need for accompanying
  44. comments describing what the value is.
  45.  
  46. Tom
  47.  
  48.  
  49.